package com.yk.govpro.service.noticeDetail.impl;

import com.yk.govpro.dao.common.api.ICommonDAO;
import com.yk.govpro.model.NoticedetailEntity;
import com.yk.govpro.service.build.api.IURLBuildService;
import com.yk.govpro.service.noticeDetail.api.INoticeDetailCrawlerService;
import com.yk.govpro.service.request.api.IJSONRequestService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zsyoung on 2017/5/22.
 */
@Service("NoticeDetailCrawlerService")
public class NoticeDetailCrawlerServiceImpl implements INoticeDetailCrawlerService {
    @Autowired
    private ICommonDAO commonDAO;
    @Autowired
    private IJSONRequestService jsonRequestService;
    @Autowired
    private IURLBuildService urlBuildService;

    @Override
    public List<JSONObject> getNoticeDetail(List<String> idList) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        List<URL> urls = this.urlBuildService.buildDetailIDURL(idList);
        for (URL url : urls) {
            jsonObjects.add(this.jsonRequestService.requestJSONObject(url));
        }
        return jsonObjects;
    }

    @Override
    public List<JSONObject> getNoticeDetail(String id) {
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (URL url : this.urlBuildService.buildDetailIDURL(id)) {
            jsonObjects.add(this.jsonRequestService.requestJSONObject(url));
        }
        return jsonObjects;
    }

    @Override
    public List<NoticedetailEntity> transform(JSONObject jsonObject) {
        List<NoticedetailEntity> entities = new ArrayList<>();
        NoticedetailEntity noticedetailEntity = new NoticedetailEntity();
        JSONObject noticeDetail = jsonObject.getJSONObject("notice");
        //  noticedetailEntity.setNoticeId(noticeDetail.getString("id"));
        noticedetailEntity.setCreatorOrgName(noticeDetail.has("creatorOraName") ? noticeDetail.getString("creatorOraName") : null);
        noticedetailEntity.setAttachments(noticeDetail.has("attachments") ?noticeDetail.getString("attachments"):null);
        noticedetailEntity.setHtml(noticeDetail.has("html") ?noticeDetail.getString("html"):null);
        noticedetailEntity.setProjectPurchaseWayName(noticeDetail.has("projectPurchaseWayName") ? noticeDetail.getString("projectPurchaseWayName") : null);
        entities.add(noticedetailEntity);
        return entities;
    }

    @Override
    public List<NoticedetailEntity> transform(List<JSONObject> jsonObjects) {
        List<NoticedetailEntity> noticedetailEntities = new ArrayList<>();
        for (JSONObject jsonObject : jsonObjects) {
            noticedetailEntities.addAll(this.transform(jsonObject));
        }
        return noticedetailEntities;
    }

    @Override
    public void saveNoticeDetail(NoticedetailEntity noticelistEntity) {
        this.commonDAO.save(noticelistEntity);

    }

    @Override
    public void saveNoticeDetail(List noticelistEntites) {
        this.commonDAO.saveObjects(noticelistEntites);

    }


}
